/**
 * 学生管理API接口
 */
import { request } from '@/utils/request'

// 学生类型定义
export interface Student {
  id: number
  user?: number
  student_id: string
  name: string
  gender: 'M' | 'F'
  phone?: string
  email?: string
  grade: {
    id: number
    name: string
  }
  status: 'active' | 'graduated' | 'suspended' | 'dropped'
  advisor?: {
    id: number
    username: string
    first_name?: string
    last_name?: string
  }
  employment_status: 'unemployed' | 'employed' | 'self_employed' | 'further_study'
  resume_status: 'not_started' | 'in_progress' | 'submitted' | 'approved' | 'rejected'
  created_at: string
  updated_at: string
}

export interface Teacher {
  id: number
  username: string
  first_name?: string
  last_name?: string
  student_count: number
}

export interface AdvisorStatistics {
  teachers: Teacher[]
  unassigned_students: number
  total_teachers: number
}

export interface StudentStatistics {
  total_students: number
  by_school: Record<string, number>
  by_major: Record<string, number>
  by_grade: Record<string, number>
  by_status: Record<string, number>
  by_employment_status: Record<string, number>
  by_gender: Record<string, number>
  recent_enrollments: Array<{
    enrollment_date: string
    count: number
  }>
  employment_rate: number
}

export interface StudentQueryParams {
  page?: number
  page_size?: number
  name?: string
  student_id?: string
  advisor_status?: 'assigned' | 'unassigned'
  grade?: number
  status?: string
}

export interface AssignAdvisorData {
  student_id: number
  advisor_id: number | null
}

export interface BatchAssignAdvisorData {
  assignments: AssignAdvisorData[]
}

// 学生API
export const studentApi = {
  // 获取学生列表
  getStudentList: (params?: StudentQueryParams) =>
    request.get('/students/students/', params),

  // 获取学生详情
  getStudentDetail: (id: number) => 
    request.get(`/students/students/${id}/`),

  // 创建学生
  createStudent: (data: Partial<Student>) => 
    request.post('/students/students/', data),

  // 更新学生
  updateStudent: (id: number, data: Partial<Student>) => 
    request.put(`/students/students/${id}/`, data),

  // 删除学生
  deleteStudent: (id: number) => 
    request.delete(`/students/students/${id}/`),

  // 获取我的学生（老师用）
  getMyStudents: () => 
    request.get('/students/my-students/'),

  // 分配指导老师
  assignAdvisor: (data: AssignAdvisorData) => 
    request.post('/students/assign-advisor/', data),

  // 批量分配指导老师
  batchAssignAdvisor: (data: BatchAssignAdvisorData) => 
    request.post('/students/batch-assign-advisor/', data),

  // 获取指导老师统计信息
  getAdvisorStatistics: () =>
    request.get('/students/advisor-statistics/'),

  // 获取学生统计数据
  getStatistics: () =>
    request.get('/students/students/statistics/'),

  // 导入学生
  importStudents: (data: FormData) =>
    request.post('/students/students/import/', data, {
      headers: { 'Content-Type': 'multipart/form-data' }
    }),

  // 导出学生
  exportStudents: (params?: StudentQueryParams) => 
    request.get('/students/students/export/', { 
      params,
      responseType: 'blob'
    })
}

// 年级API
export const gradeApi = {
  // 获取年级列表
  getGradeList: () => 
    request.get('/students/grades/'),

  // 获取年级详情
  getGradeDetail: (id: number) => 
    request.get(`/students/grades/${id}/`),

  // 创建年级
  createGrade: (data: { name: string; description?: string }) => 
    request.post('/students/grades/', data),

  // 更新年级
  updateGrade: (id: number, data: { name: string; description?: string }) => 
    request.put(`/students/grades/${id}/`, data),

  // 删除年级
  deleteGrade: (id: number) => 
    request.delete(`/students/grades/${id}/`)
}

// 专业API
export const majorApi = {
  // 获取专业列表
  getMajorList: () => 
    request.get('/students/majors/'),

  // 获取专业详情
  getMajorDetail: (id: number) => 
    request.get(`/students/majors/${id}/`),

  // 创建专业
  createMajor: (data: { name: string; description?: string }) => 
    request.post('/students/majors/', data),

  // 更新专业
  updateMajor: (id: number, data: { name: string; description?: string }) => 
    request.put(`/students/majors/${id}/`, data),

  // 删除专业
  deleteMajor: (id: number) => 
    request.delete(`/students/majors/${id}/`)
}

// 校区API
export const schoolApi = {
  // 获取校区列表
  getSchoolList: () => 
    request.get('/students/schools/'),

  // 获取校区详情
  getSchoolDetail: (id: number) => 
    request.get(`/students/schools/${id}/`),

  // 创建校区
  createSchool: (data: { 
    name: string
    code: string
    principal?: string
    phone?: string
    address?: string
  }) => 
    request.post('/students/schools/', data),

  // 更新校区
  updateSchool: (id: number, data: { 
    name: string
    code: string
    principal?: string
    phone?: string
    address?: string
  }) => 
    request.put(`/students/schools/${id}/`, data),

  // 删除校区
  deleteSchool: (id: number) =>
    request.delete(`/students/schools/${id}/`)
}

// 教师API
export const teacherApi = {
  // 获取教师列表
  getTeacherList: () =>
    request.get('/accounts/teachers/'),

  // 获取简化教师列表（用于下拉框）
  getSimpleTeacherList: () =>
    request.get('/students/teachers/'),

  // 获取教师详情
  getTeacherDetail: (id: number) =>
    request.get(`/accounts/teachers/${id}/`),

  // 创建教师
  createTeacher: (data: {
    username: string
    password: string
    first_name?: string
    last_name?: string
    email?: string
    phone?: string
  }) =>
    request.post('/accounts/teachers/', data),

  // 更新教师
  updateTeacher: (id: number, data: {
    first_name?: string
    last_name?: string
    email?: string
    phone?: string
  }) =>
    request.put(`/accounts/teachers/${id}/`, data),

  // 删除教师
  deleteTeacher: (id: number) =>
    request.delete(`/accounts/teachers/${id}/`)
}

// 常量定义
export const STUDENT_STATUS_OPTIONS = [
  { value: 'active', label: '在校' },
  { value: 'graduated', label: '已毕业' },
  { value: 'suspended', label: '休学' },
  { value: 'dropped', label: '退学' }
]

export const EMPLOYMENT_STATUS_OPTIONS = [
  { value: 'unemployed', label: '未就业' },
  { value: 'employed', label: '已就业' },
  { value: 'self_employed', label: '自主创业' },
  { value: 'further_study', label: '继续深造' }
]

export const RESUME_STATUS_OPTIONS = [
  { value: 'not_started', label: '未开始' },
  { value: 'in_progress', label: '制作中' },
  { value: 'submitted', label: '已提交' },
  { value: 'approved', label: '已通过' },
  { value: 'rejected', label: '需修改' }
]

export const GENDER_OPTIONS = [
  { value: 'M', label: '男' },
  { value: 'F', label: '女' }
]
